const $ = layui.jquery;
const table=layui.table;
const laydate=layui.laydate;
const form=layui.form;
const layer=layui.layer;
const upload=layui.upload;
$(()=> {
    if(error){
        layer.msg(error);
        return;
    }
//     渲染出生日期
    laydate.render({
        elem:"[name=birthday]"
    })
    //     渲染图片上传
    upload.render({
        elem: '#avatar-upload-btn',
        url: ctx+'/sys/api/v1/upload/image',
        data:{//传的数据
            type:"member_avatar",
        },
        exts:'jpg|png|gif|bmp|jpeg|svg', //上传的类型
        size:1024,
        done: function(resp){//上传完成，响应200状态码进入
            let url =ctx+"/assets/"+resp.data;
            $(".avatar-preview").css("background-image","url("+url+")");
            sessionStorage.setItem("avatar",resp.data);//将图片地址存到会话存储域
        },
        error: function(){
            layer.msg("上传图片失败");
        },

    });
    // 回显数据
    member.password="";
    form.val("edit-form",member);
    if(member.avatar){
        let url = ctx + "/assets/"+member.avatar;
        $(".avatar-preview").css("background-image","url("+url+")");
        sessionStorage.setItem("avatar",member.avatar);
    }


})
// 点击提交按钮后的异步提交操作
function doSubmit(cb) {
    if(error){
        layer.msg(error);
        return;
    }
    let pass = form.validate("#edit-form")//参数验证
    if (!pass) {
        return;
    }
    // 验证通过，向后台发请求
    const url = ctx + "/sys/api/v1/member";
    let formData = form.val("edit-form");//参数为过滤器
    formData.id = member.id;
    let avatarUrl=sessionStorage.getItem("avatar");
    if(avatarUrl!=null){
        formData.avatar=avatarUrl;
    }
    $.ajax({
        url,
        method: "put",
        data: formData,
        success(resp) {
            if (resp.success) {
                if (typeof cb === "function") {
                    cb(resp.success)
                }
            }
        }, error(resp) {
            let json = resp.responseJSON;
            let msg = json.msg || "修改会员异常";
            layer.msg(msg);
        }
    })

}